package evs.sample;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Store;

import evs.location.RESTful;

@RESTful
@Entity
@Indexed
public class Person {

    @Id
	@GeneratedValue
	private Long id;
	
    @Field(index=Index.UN_TOKENIZED, store=Store.NO)
	private String prename;
    @Field(index=Index.UN_TOKENIZED, store=Store.NO)
	private String surname;
    @Field(index=Index.TOKENIZED, store=Store.NO)
	private String description;
	
	@ManyToMany( fetch = FetchType.EAGER )
	@IndexedEmbedded(depth=1)
	private List<Skill> skills = new ArrayList<Skill>();
	
	@OneToMany( mappedBy = "person" )
	@IndexedEmbedded(depth=1)
	private List<Occupation> occupations = new ArrayList<Occupation>();
	
	
	public long getId() { return id; }
    
    public String getPrename() { return prename; }
    public void setPrename( String prename ) { this.prename = prename; }
    
    public String getSurname() { return surname; }
    public void setSurname( String surname ) { this.surname = surname; }
    
    public String getDescription() { return description; }
    public void setDescription( String description ) { this.description = description; }
    
    public List<Skill> getSkills() { return new ArrayList<Skill>( this.skills ); }
    public void setSkills( List<Skill> skills ) { this.skills = new ArrayList<Skill>( skills ); }
    
    public List<Occupation> getOccupations() { return Collections.unmodifiableList( this.occupations ); }
    public void setOccupations( List<Occupation> occupations ) { this.occupations = new ArrayList<Occupation>( occupations ); }
    
    public void addSkill( Skill skill )
    {
        this.skills.add( skill );
    }
    
    public void addOccupation( Occupation occupation )
    {
        assert occupation.getPerson().getId() == this.getId();
        this.occupations.add( occupation );
    }
}
